package crmdna.client;

import static crmdna.common.OfyService.ofy;

import java.util.ArrayList;
import java.util.List;

import crmdna.common.APIException;
import crmdna.common.APIResponse.Status;
import crmdna.common.Utils;
import crmdna.crmdna.Client;

public class CustomFields {
	public static class CustomFieldProp {
		int id;
		String name;
		boolean enabled; 
	}
	
	private final static String KEY = "KEY";
	
	static void create(String client, String fieldName) {
		
		Client.ensureValidClient(client);
		
		CustomFieldsEntity entity = getCustomFieldsEntity(client);
		
		if (entity == null) {
			entity = new CustomFieldsEntity();
			entity.key = KEY;
			entity.fieldNames.add(fieldName);			
			ofy(client).save().entity(entity).now();
			return;
		}
		
		//throw error if already present
		if (true == Utils.isPresentInListCaseInsensitive(entity.fieldNames, fieldName)) 
			throw new APIException(Status.ERROR_RESOURCE_ALREADY_EXISTS, 
					"Custom field [" + fieldName + " already exists");
		
		//add the new custom field
		entity.fieldNames.add(fieldName);		
		ofy(client).save().entity(entity).now();
	}
	
	static List<CustomFieldProp> getAll(String client) {
		Client.ensureValidClient(client);
		
		List<CustomFieldProp> list = new ArrayList<CustomFieldProp>();
		
		CustomFieldsEntity entity = getCustomFieldsEntity(client);
		
		if (null == entity)
			return list;
		
		for (int i = 0; i < entity.fieldNames.size(); i++) {
			CustomFieldProp customFieldProp = new CustomFieldProp();
			
			customFieldProp.id = i;
			customFieldProp.name = entity.fieldNames.get(i);			
			customFieldProp.enabled = true; 
			if (entity.disabledFieldIds.contains(i))
				customFieldProp.enabled = false;
			
			list.add(customFieldProp);
		}
		
		return list;				
	}
	
	static void update(String client, int fieldId, String newFieldName, Boolean enabled) {
		
		Client.ensureValidClient(client);
		
		CustomFieldsEntity entity = getCustomFieldsEntity(client);
		
		if (entity == null) {
			throw new APIException(Status.ERROR_RESOURCE_DOES_NOT_EXIST, 
					"No custom field found");
		}
		
		if (fieldId > entity.fieldNames.size() - 1)
			throw new APIException(Status.ERROR_RESOURCE_DOES_NOT_EXIST, 
				"There is no custom field with id [" + fieldId + "]");
		
		if (! entity.fieldNames.get(fieldId).equalsIgnoreCase(newFieldName)) {
			//not a rename, check if new name does not exist
			if (true == Utils.isPresentInListCaseInsensitive(entity.fieldNames, newFieldName))
				throw new APIException(Status.ERROR_RESOURCE_ALREADY_EXISTS, 
						"Field name [" + newFieldName + "] is already present");			
		}
		
		//null will preserve existing value
		if (null != newFieldName)
			entity.fieldNames.set(fieldId, newFieldName);
		
		if (null != enabled) {
			if (enabled)
				entity.disabledFieldIds.remove(fieldId);
			else 
				entity.disabledFieldIds.add(fieldId);
		}
				
		ofy(client).save().entity(entity).now();			
	}
			
	private static CustomFieldsEntity getCustomFieldsEntity(String client) {
		CustomFieldsEntity entity = ofy(client).load()
			.type(CustomFieldsEntity.class).id(KEY).get();
		
		return entity;
	}
}
